﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Catering.yl.Daily
{
    public partial class BanzuDynamic : System.Web.UI.Page
    {        
        protected void Page_Load(object sender, EventArgs e)
        {            
            if(!Page.IsPostBack)
            {
                string useDate = null;
                string currentClassID = null;
                string currentGroupID = null;
                if (Session["useDate"] != null)
                    useDate = Session["useDate"].ToString();
                if (Session["ClassID"] != null)
                    currentClassID = Session["ClassID"].ToString();
                if (Session["GroupID"] != null)
                    currentGroupID = Session["GroupID"].ToString();

                if (RB1.Checked)
                {
                    shebeijiankong.Text = "正常";
                    shebeijiankong.Enabled = false;
                }   
                if( Session["ClassID"]!=null || Session["GroupID"]!=null || Session["useDate"] != null )
                {
                    try
                    {
                        DataTable dt_Plan = new Catering_Rules.BanzuDynamic().GetList("ClassID='" + currentClassID + "'").Tables[0];
                        KitchenNum.Text = dt_Plan.Rows[0][1].ToString();
                        KitchenArea.Text = dt_Plan.Rows[0][2].ToString();
                        qiyouzao.Text= dt_Plan.Rows[0][3].ToString();
                        diancizao.Text= dt_Plan.Rows[0][4].ToString();
                        penlin.Text= dt_Plan.Rows[0][5].ToString();
                        miehuoqi.Text= dt_Plan.Rows[0][6].ToString();
                        miehuotan.Text= dt_Plan.Rows[0][7].ToString();
                        shebeijiankong.Text= dt_Plan.Rows[0][8].ToString();
                        if (dt_Plan.Rows[0][8].ToString() == "正常")
                        {
                            RB1.Checked = true;
                            RB2.Checked = false;
                            shebeijiankong.Enabled = false;
                        }
                        else
                        {
                            RB1.Checked = false;
                            RB2.Checked = true;
                            shebeijiankong.Enabled = true;
                        }
                        gaoweishebei.Text= dt_Plan.Rows[0][9].ToString();
                        shebeishuliang.Text= dt_Plan.Rows[0][10].ToString();
                        xiyishebei.Text= dt_Plan.Rows[0][11].ToString();
                        youyanji.Text= dt_Plan.Rows[0][12].ToString();
                        lengcangku.Text= dt_Plan.Rows[0][13].ToString();
                        dongku.Text= dt_Plan.Rows[0][14].ToString();
                        ganhuoku.Text = dt_Plan.Rows[0][15].ToString();
                        txt_zuoyerenshu.Text = dt_Plan.Rows[0]["zuoyerenshu"].ToString();
                        ddl_zuoyeleixing.Text = dt_Plan.Rows[0]["zuoyeleixing"].ToString();
                        txt_zuoyezhouqi.Text = dt_Plan.Rows[0]["zuoyezhouqi"].ToString();
                    }
                    catch
                    {

                    }
                }
            }
        }
        [WebMethod]
        public static string Save_BanzuDynamic(string KitchenNum, string KitchenArea, string qiyouzao, string diancizao, string penlin, string miehuoqi, string miehuotan, string shebeijiankong, string gaoweishebei, string shebeishuliang, string xiyishebei, string youyanji, string lengcangku, string dongku, string ganhuoku, string txt_zuoyerenshu, string ddl_zuoyeleixing, string txt_zuoyezhouqi, string hdn_useDate, string hdn_currentClassID, string hdn_currentGroupID)
        {    
            //删除数据
            try
            {
                new Catering_Rules.BanzuDynamic().Delete("saveDate='" + Convert.ToDateTime(hdn_useDate).ToString() + "' and ClassID=" + hdn_currentClassID);
                Catering_Model.BanzuDynamic _ce = new Catering_Model.BanzuDynamic();
                _ce.KitchenNum = int.Parse(KitchenNum);
                _ce.KitchenArea = float.Parse(KitchenArea);
                _ce.qiyouzao = int.Parse(qiyouzao);
                _ce.diancizao = int.Parse(diancizao);
                _ce.penlin = int.Parse(penlin);
                _ce.miehuoqi = int.Parse(miehuoqi);
                _ce.miehuotan = int.Parse(miehuotan);
                _ce.shebeijiankong = shebeijiankong;
                _ce.gaoweishebei = gaoweishebei;
                _ce.shebeishuliang = int.Parse(shebeishuliang);
                _ce.xiyishebei = int.Parse(xiyishebei);
                _ce.youyanji = int.Parse(youyanji);
                _ce.lengcangku = lengcangku;
                _ce.dongku = dongku;
                _ce.ganhuoku = ganhuoku;
                _ce.zuoyerenshu = txt_zuoyerenshu;
                _ce.zuoyeleixing = ddl_zuoyeleixing;
                _ce.zuoyezhouqi = txt_zuoyezhouqi;
                _ce.ClassID = int.Parse(hdn_currentClassID);
                _ce.savedate = Convert.ToDateTime(hdn_useDate);


                new Catering_Rules.BanzuDynamic().Add(_ce);
                return "OK";
            }
            catch(Exception ex)
            {
                return "FALSE";
            }           
        }
        //验证日期内是否存在数据
        [WebMethod]
        public static string IsExists(string hdn_useDate, string hdn_currentClassID, string hdn_currentGroupID)
        {
            DataTable dt = new Catering_Rules.BanzuDynamic().GetList("savedate='" + Convert.ToDateTime(hdn_useDate).ToString() + "' and ClassID=" + hdn_currentClassID).Tables[0];
            return (dt != null && dt.Rows.Count > 0) ? "OK" : "NO";
        }
        protected void RB1_CheckedChanged(object sender, EventArgs e)
        {
            if (RB1.Checked)
            {
                shebeijiankong.Text = "正常";
                shebeijiankong.Enabled = false;
            }                
        }

        protected void RB2_CheckedChanged(object sender, EventArgs e)
        {
            if (RB2.Checked)
            {
                shebeijiankong.Text = "";
                shebeijiankong.Enabled = true;
            }                
        }
        protected void btn_type_Click(object sender, EventArgs e)
        {
            ddl_zuoyeleixing.Text = "";
            int nItems = 0;
            foreach(ListItem li in cbl_type.Items )
            {
                if (li.Selected == true)
                    nItems++;
            }
            if(nItems == 0)
            {
                string strError = "alert('请选择正确的作业类型！')";
                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "onekey", strError, true);
                return;
            }
            else
            {
                for (int nCount = 0; nCount < cbl_type.Items.Count; nCount++)
                {
                    if (cbl_type.Items[nCount].Selected == true && cbl_type.Items[nCount].Text != "其它")
                        ddl_zuoyeleixing.Text += cbl_type.Items[nCount].Text + ",";
                    if (cbl_type.Items[nCount].Selected == true && cbl_type.Items[nCount].Text == "其它")
                        ddl_zuoyeleixing.Text += txt_Other.Text + ",";
                }
                ddl_zuoyeleixing.Text = ddl_zuoyeleixing.Text.Substring(0, ddl_zuoyeleixing.Text.Length-1);
            } 
            main.Visible = false;
        }

        protected void type_list_Click(object sender, EventArgs e)
        {
            main.Visible = true;
            foreach (ListItem li in cbl_type.Items)
            {
                li.Selected = false;
            }
        }

        protected void cbl_type_SelectedIndexChanged(object sender, EventArgs e)
        {
            foreach (ListItem li in cbl_type.Items)
            {
                if (li.Text == "其它" && li.Selected == true)
                    txt_Other.Visible = true;
                if (li.Text == "其它" && li.Selected == false)
                    txt_Other.Visible = false;
            }
        }

        protected void btn_Close_Click(object sender, EventArgs e)
        {
            main.Visible = false;
        }
    }
}